package com.ruoyi.message.service.impl;

import com.ruoyi.common.constant.MeetMessage;
import com.ruoyi.common.constant.PlayerData;
import com.ruoyi.message.handler.WebSocketHandler;
import com.ruoyi.message.service.RoomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.websocket.Session;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: 高飞
 * @time: 2023/12/2
 * @description: 类描述
 **/
@Service
public class RoomServiceImpl implements RoomService {
    private static final Logger log = LoggerFactory.getLogger(RoomServiceImpl.class);

    // key 链接 value会议编码
    private final Map<Session, Room> sessionRoomMap = new ConcurrentHashMap<>();

    private final Map<Long, Room> roomMap = new ConcurrentHashMap<>();

    @Override
    public List<PlayerData> getPlayer(Long roomId) {
        Room room = roomMap.get(roomId);
        if (Objects.isNull(room)) {
            return new ArrayList<>(0);
        }
        return room.listPlayer();
    }

    /**
     * 进入房间
     * author 高飞
     * time   2023/12/2
     * @param roomId 房间编号
     * @param userName 用户名
     * @param session 链接
     */
    @Override
    public synchronized void join(Long roomId, String userName, Session session) {
        if (roomMap.containsKey(roomId)) {
            Room room = roomMap.get(roomId);
            sessionRoomMap.put(session, room);
            room.join(userName, session);
        }
        else {
            Room room = roomMap.computeIfAbsent(roomId, v -> new Room(roomId));
            sessionRoomMap.put(session, room);
            room.join(userName, session);
        }
    }

    /**
     * 离开房间
     * author 高飞
     * time   2023/12/2
     * @param userName 用户名
     * @param session 链接
     */
    @Override
    public synchronized void leave(String userName, Session session) {
        Room room = sessionRoomMap.remove(session);
        if (Objects.nonNull(room)) {
            room.leave(session);
            if (room.peopleNum() == 0) {
                roomMap.remove(room.getRoomId());
            }
        }
    }

    @Override
    public void beginScreenShare(Session session, MeetMessage meetMessage) {
        Room room = sessionRoomMap.get(session);
        if (Objects.nonNull(room)) {
            room.beginScreenShare(session, meetMessage);
        }
    }

    @Override
    public void binMessage(Session session, byte[] bytes) {
        Room room = sessionRoomMap.get(session);
        if (Objects.nonNull(room)) {
            room.binMessage(session, bytes);
        }
    }

    /**
     * 给会议内的人发送消息
     * author 高飞
     * time   2023/12/2
     * @param session 链接
     * @param meetMessage 消息体
     */
    @Override
    public void messageToOther(Session session, MeetMessage meetMessage) {
        Room room = sessionRoomMap.get(session);
        if (Objects.nonNull(room)) {
            room.messageToOther(session, meetMessage);
        }
    }

    /**
     * 给会议内的人发送消息
     * author 高飞
     * time   2023/12/2
     * @param session 链接
     * @param meetMessage 消息体
     */
    @Override
    public void messageToOther(Session session, byte[] meetMessage) {
        Room room = sessionRoomMap.get(session);
        if (Objects.nonNull(room)) {
            room.messageToOther(session, meetMessage);
        }
    }
}
